﻿#ifndef _CURL_UTILS_H_
#define _CURL_UTILS_H_

#include <string>
#include <curl/curl.h>

struct UrlObeject {
    const char* url;
    long size;
    // 文件地址
    const char* filePath;
    std::string uuid;
    UrlObeject(const char* url, const char* path);
};

// 定义全局变量
namespace HNNetwork {
#define MAX1024 (1024)
    bool CheckUrlCode(CURL* curl, const char* url, CURLcode status);
    bool JustCheckUrlCode(CURL* curl, const char* url, CURLcode status);
    std::string convertDataSize(long size_bits);
    std::string getNetworkRate(long size_bits, long time_ms);
#define MAXFLOW (1024*1024) // 最大流量kb
#define MINFLOW 16 //最小流量kb
#define MAXTHREADCOUNT 100 //最大线程数量
#define LIMITMAX(curFlow,maxFlow) if(curFlow > maxFlow) {curFlow = maxFlow;}
#define LIMITMIN(curFlow,minFlow) if(curFlow < minFlow) {curFlow = minFlow;}

#define ASSERT_CURLCODE(curl,url,res) if(!CheckUrlCode(curl,url,res)) {return;}
    //#define ASSERT_CURLCODE_RETURN_FALSE(curl,url,res) if(!CheckUrlCode(curl,url,res)) return false;
#define ASSERT_CURLCODE_CONTINUE(curl,url,res) if(!JustCheckUrlCode(curl,url,res)) {continue;}

    // 回调函数，用于保存下载的数据
    size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp);
    void MessageCallbackThread(const std::string& url, long threadID, int status, const char* msg);
   
    // 获取需要开辟的线程数量
    long getThreadCount(long size, long curFlow);
}//HNNetWork
#endif
